﻿using System;
using System.Collections.Generic;
using System.Text;
using Lon.Util;
using System.Windows.Forms;
using System.Data;

namespace  Lon.Server
{
    public partial class MainForm 
    {
        private void AddUserLog(string p, DateTime beginTime, DateTime endTime)
        {

        }

        private void mnuUserLog_Click(object sender, EventArgs e)
        {
            //用户操作日志
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            UserLogForm userLogForm = new UserLogForm();
            userLogForm.ShowDialog();

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("用户操作日志", beginTime, endTime);
        }


        private void mnuUserMan_Click(object sender, EventArgs e)
        {
            //用户管理
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            UserManForm userManForm = new UserManForm();
            userManForm.ShowDialog();

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("用户管理", beginTime, endTime);

        }

        private void mnuLogin_Click(object sender, EventArgs e)
        {
            //登陆
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            string userName = "未登陆用户";
            bool boState = false;

            LoginForm loginForm = new LoginForm();

            if (loginForm.ShowDialog() == DialogResult.OK)
            {
                userName = loginForm.UserName;

                string password = loginForm.Password;

                password = HashHelper.GetTextHash(password);

                boState = CheckUser(userName, password);

                if (boState)
                {
                    int level = GetLevel(userName);
                    LoginMan.Instance.LoginSuccess = true;
                    LoginMan.Instance.LoginUser = userName;
                    LoginMan.Instance.LoginPrivilege = (PrivilegeType)level;
                }
                else
                {
                    LoginMan.Instance.LoginUser = "未登陆用户";
                    LoginMan.Instance.LoginSuccess = false;
                    LoginMan.Instance.LoginPrivilege = PrivilegeType.NONE;
                    MessageBox.Show(" 用户名或密码错误！   ", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                UpdatePrivilegeView();

            }

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog(string.Format("[{0}]登陆{1}!", userName, (boState ? "成功" : "失败")), beginTime, endTime);


        }


        void UpdatePrivilegeView()
        {
            //更新用户权限允许开放的项目
            PrivilegeType type = LoginMan.Instance.LoginPrivilege;
            mnuConfigEdit.Enabled = PrivilegeMan.Test(type, PrivilegeType.EDIT_CONFIG);
            mnuUserMan.Enabled = PrivilegeMan.Test(type, PrivilegeType.EDIT_USER);
            mnuLogError.Enabled = PrivilegeMan.Test(type, PrivilegeType.VIEW_ERROR);
            mnuLogInfo.Enabled = PrivilegeMan.Test(type, PrivilegeType.VIEW_INFO);
            mnuUserLog.Enabled = PrivilegeMan.Test(type, PrivilegeType.VIEW_USER_LOG);
            mnuSaveIPList.Enabled = PrivilegeMan.Test(type, PrivilegeType.SAVE_IPLIST);
            mnuDiagnoSystem.Enabled = PrivilegeMan.Test(type, PrivilegeType.SYS_DIAGNO);
            mnuExit.Enabled = PrivilegeMan.Test(type, PrivilegeType.SYS_CLOSE);
            pmnuExit.Enabled = true;// mnuExit.Enabled;

            statusLabel3.Text = LoginMan.Instance.LoginUser;

        }
        private void mnuLogInfo_Click(object sender, EventArgs e)
        {
            //系统日志
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            LogViewForm logViewForm = new LogViewForm();
            logViewForm.LogType = LogViewType.Info;
            logViewForm.ShowDialog();

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("系统日志", beginTime, endTime);
        }

        private void mnuLogError_Click(object sender, EventArgs e)
        {
            //异常日志
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            LogViewForm logViewForm = new LogViewForm();
            logViewForm.LogType = LogViewType.Error;
            logViewForm.ShowDialog();

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("异常日志", beginTime, endTime);
        }

        private void mnuConfigEdit_Click(object sender, EventArgs e)
        {
            //配置文件
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            ConfigEditForm configEditForm = new ConfigEditForm();
            configEditForm.ShowDialog();

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("配置文件", beginTime, endTime);

        }


        private int GetLevel(string userName)
        {
            return 0;
        }

        private bool CheckUser(string userName, string password)
        {
            return true;
        }

        private void mnuDiagnoSystem_Click(object sender, EventArgs e)
        {
            //系统自检
            DateTime beginTime = DateTime.Now;
            //--------------------------------

            DiagnoForm diagnoForm = new DiagnoForm();
            diagnoForm.ShowDialog();

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("系统自检", beginTime, endTime);
        }

        #region 主菜单
        private void mnuSaveIPList_Click(object sender, EventArgs e)
        {
            //保存IP列表
            if (dtNetList.Rows.Count < 1) return;

            DateTime beginTime = DateTime.Now;
            //--------------------------------

            string fileName = "IPList.txt";

            using (SaveFileDialog dlgSave = new SaveFileDialog())
            {
                dlgSave.FileName = fileName;
                dlgSave.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                if (dlgSave.ShowDialog() != DialogResult.OK) return;
                fileName = dlgSave.FileName;
            }

            try
            {
                StringBuilder sb = new StringBuilder();

                for (int j = 0; j < dtNetList.Rows.Count; j++)
                {
                    DataRow row = dtNetList.Rows[j];

                    string dbm = row[ID_DBM].ToString();
                    string ip = row[ID_IP].ToString();
                    string nodeType = row[ID_NAME].ToString();

                    string textLine = string.Format("{0}    {1}    {2}", dbm, ip, nodeType);

                    sb.AppendLine(textLine);
                }

                System.IO.File.WriteAllText(fileName, sb.ToString(), Encoding.Default);

            }
            catch
            {
            }

            //--------------------------------
            DateTime endTime = DateTime.Now;
            AddUserLog("保存IP列表", beginTime, endTime);
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            Close();
            //Application.Exit();
        }

        int panelIndex = 0;
        private void mnuViewData_Click(object sender, EventArgs e)
        {
            panelIndex++;
            if (panelIndex > 2) panelIndex = 1;

            if (panelIndex == 1)
            {
                //1: max
                splitContainer1.Panel1Collapsed = false;
                splitContainer1.Panel2Collapsed = true;
            }
            else if (panelIndex == 2 )
            {
                //1, 2: middle
                splitContainer1.Panel1Collapsed = false;
                splitContainer1.Panel2Collapsed = false;
            }
            else 
            {
                return;
            }

        }



        private void mnuAbout_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(string.Format("  集中监测 - {0}      \r\n\r\n     版本: {1}", ID_APP_TEXT, AssemblyVersion), "关于...", MessageBoxButtons.OK, MessageBoxIcon.Information);
            MessageBox.Show(string.Format("  集中监测 - {0}            \r\n\r\n     版本： {1}         \r\n\r\n     日期： {2}", ID_APP_TEXT, AssemblyVersion, BuildDate), "关于...", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        #endregion

    }
}
